using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MediaController.Utils.UI
{
    public class TextInputDialog : Form
    {

        private readonly Container components;
        private Button _canxButton;
        private string _defaultValue = string.Empty;
        private TextBox _inputValue;
        private Button _okButton;
        private Button _resetButton;


        public TextInputDialog()
        {
            InitializeComponent();
        }

        public char PasswordChar
        {
            get { return _inputValue.PasswordChar; }
            set { _inputValue.PasswordChar = value; }
        }

        public string Prompt
        {
            get { return Text; }
            set { Text = value; }
        }

        public string Value
        {
            get { return _inputValue.Text; }
            set
            {
                if (value != _inputValue.Text)
                {
                    _inputValue.Text = value;
                    _inputValue.SelectionStart = value.Length;
                    _inputValue.SelectionLength = 0;
                    _defaultValue = value;
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            _inputValue = new TextBox();
            _okButton = new Button();
            _canxButton = new Button();
            _resetButton = new Button();
            SuspendLayout();
            // 
            // InputValue
            // 
            _inputValue.Location = new Point(0, 0);
            _inputValue.Name = "_inputValue";
            _inputValue.Size = new Size(304, 20);
            _inputValue.TabIndex = 1;
            // 
            // OKButton
            // 
            _okButton.Anchor = ((((AnchorStyles.Top | AnchorStyles.Bottom)
                                  | AnchorStyles.Left)));
            _okButton.DialogResult = DialogResult.OK;
            _okButton.FlatStyle = FlatStyle.System;
            _okButton.Location = new Point(40, 26);
            _okButton.Name = "_okButton";
            _okButton.Size = new Size(64, 24);
            _okButton.TabIndex = 2;
            _okButton.Text = "OK";
            // 
            // CanxButton
            // 
            _canxButton.Anchor = ((((AnchorStyles.Top | AnchorStyles.Bottom)
                                    | AnchorStyles.Left)));
            _canxButton.DialogResult = DialogResult.Cancel;
            _canxButton.FlatStyle = FlatStyle.System;
            _canxButton.Location = new Point(184, 26);
            _canxButton.Name = "_canxButton";
            _canxButton.Size = new Size(64, 24);
            _canxButton.TabIndex = 3;
            _canxButton.Text = "Cancel";
            // 
            // ResetButton
            // 
            _resetButton.Anchor = ((((AnchorStyles.Top | AnchorStyles.Bottom)
                                     | AnchorStyles.Left)));
            _resetButton.FlatStyle = FlatStyle.System;
            _resetButton.Location = new Point(112, 26);
            _resetButton.Name = "_resetButton";
            _resetButton.Size = new Size(64, 24);
            _resetButton.TabIndex = 4;
            _resetButton.Text = "Reset";
            _resetButton.Click += ResetButton_Click;
            // 
            // TextInputDialog
            // 
            AcceptButton = _okButton;
            AutoScaleBaseSize = new Size(5, 13);
            CancelButton = _canxButton;
            ClientSize = new Size(304, 54);
            ControlBox = false;
            Controls.Add(_resetButton);
            Controls.Add(_canxButton);
            Controls.Add(_okButton);
            Controls.Add(_inputValue);
            KeyPreview = true;
            MaximizeBox = false;
            MinimizeBox = false;
            Name = "TextInputDialog";
            StartPosition = FormStartPosition.Manual;
            Text = "TextInputDialog";
            KeyDown += TextInputDialog_KeyDown;
            ResumeLayout(false);
            PerformLayout();
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            _inputValue.Text = _defaultValue;
        }

        public void SelectAll()
        {
            _inputValue.SelectAll();
        }

        private void TextInputDialog_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                DialogResult = DialogResult.Cancel;
            }
        }
    }
}